package com.qst.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qst.common.annotation.Log;
import com.qst.common.core.controller.BaseController;
import com.qst.common.core.domain.AjaxResult;
import com.qst.common.enums.BusinessType;
import com.qst.system.domain.News;
import com.qst.system.service.INewsService;
import com.qst.common.core.page.TableDataInfo;
import com.qst.common.utils.poi.ExcelUtil;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 宣传新闻Controller
 * 
 * @author linzhe
 * @date 2025-08-25
 */
@RestController
@RequestMapping("/system/news")
public class NewsController extends BaseController
{
    @Autowired
    private INewsService newsService;

    /**
     * 查询宣传新闻列表
     */
    @PreAuthorize("@ss.hasPermi('system:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(News news)
    {
        startPage();
        List<News> list = newsService.selectNewsList(news);
        return getDataTable(list);
    }

    /**
     * 导出宣传新闻列表
     */
    @PreAuthorize("@ss.hasPermi('system:news:export')")
    @Log(title = "宣传新闻", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(News news)
    {
        List<News> list = newsService.selectNewsList(news);
        ExcelUtil<News> util = new ExcelUtil<News>(News.class);
        return util.exportExcel(list, "宣传新闻数据");
    }

    /**
     * 获取宣传新闻详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:news:query')")
    @GetMapping(value = "/{nId}")
    public AjaxResult getInfo(@PathVariable("nId") Long nId)
    {
        try {
            return AjaxResult.success(newsService.selectNewsById(nId));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 新增宣传新闻
     */
    @PreAuthorize("@ss.hasPermi('system:news:add')")
    @Log(title = "宣传新闻", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody News news)
    {
        try {
            return toAjax(newsService.insertNews(news));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改宣传新闻
     */
    @PreAuthorize("@ss.hasPermi('system:news:edit')")
    @Log(title = "宣传新闻", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody News news)
    {
        try {
            return toAjax(newsService.updateNews(news));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除宣传新闻
     */
    @PreAuthorize("@ss.hasPermi('system:news:remove')")
    @Log(title = "宣传新闻", businessType = BusinessType.DELETE)
    @DeleteMapping("/{nIds}")
    public AjaxResult remove(@PathVariable Long[] nIds)
    {
        try {
            return toAjax(newsService.deleteNewsByIds(nIds));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 全局异常处理方法
     */
    @ExceptionHandler(Exception.class)
    public AjaxResult handleException(Exception ex) {
        return AjaxResult.error(ex.getMessage());
    }
}